package com.ganinfo.common.rsms;



import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.ganinfo.common.constant.PropertiesConstant;
import com.ganinfo.common.rsms.model.MeetingSms;

/**
 * 荣联云短信下发接口
 * @author Administrator
 * 2015年12月21日
 */
public class Rsms {

	private static final Logger log = LoggerFactory.getLogger(Rsms.class);
	
	private static final String proIP = "app.cloopen.com";
	private static final String port = "8883";
	/**获取验证码**/
	public static final String Identyfy = "135096";//108201
	/**获取验证码(智慧医疗)**/
	public static final String Identyfy_Healthcare = "135096";//108201
	/**获取验证码(智慧医疗挂号停诊)**/
	public static final String Identyfy_Stop = "156994";
	/**获取验证码(智慧医疗挂号取消)**/
	public static final String Identyfy_Cancel = "156993";
	/**获取验证码(智慧医疗挂号预约挂号)**/
	public static final String Identyfy_Appointment = "156992";
	/**修改密码**/
	public static final String Passwd = "68231";
	
	public static final String Collection = "108209";

	/**通知**/
	public static final String MeetingNum = "104703";

	/**
	 * @Title: sendAppointmentTemplateSMS
	 * @Description: 智慧医疗-预约挂号
	 * @param para
	 * @return
	 * @return: String
	 */
	public static String sendAppointmentTemplateSMS(Map<String,Object> para){
		String res = "";
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		String phone = para.get("phone").toString();
		String temp = para.get("temp").toString();
		log.info("容联云短信发送会议通知 phone:"+phone+";模版ID:"+temp);
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		//患者姓名
		String paientname = para.get("paientname").toString();
		//就诊时间
		String visitdate = para.get("visitdate").toString();
		//上下午
		String ampm = para.get("ampm").toString();
		//医院名称
		String hospitalname = para.get("hospitalname").toString();
		//科室、子科室名称
		String subdeptname = para.get("subdeptname").toString();
		//医生姓名
		String docname = para.get("docname").toString();
		//挂号费
		String sourceregfee = para.get("sourceregfee").toString();
		switch(temp){
			case Identyfy_Appointment:
				result = restAPI.sendTemplateSMS(phone,temp,new String[]{paientname,visitdate+"(预约日期)"+ampm+"(上下午)",
						                                             hospitalname+"-"+subdeptname+"-"+subdeptname+"-"+docname+"(医院-科室-科室-医生)",
						                                             sourceregfee});
				break;
			case Identyfy_Cancel:
			case Identyfy_Stop:
				result = restAPI.sendTemplateSMS(phone,temp,new String[]{paientname,visitdate+"(预约日期)"+ampm+"(上下午)",
                        hospitalname+"-"+subdeptname+"-"+subdeptname+"-"+docname+"(医院-科室-科室-医生)"});
				break;
			default:
			
		}
		if("000000".equals(result.get("statusCode"))){			
			res = Boolean.TRUE.toString();
			log.info("荣联云通讯Rsms return result :" +result);
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	
	/**
	 * 容联云短信发送会议通知
	 * @param initiator	发起人
	 * @param meetingname	会议名称
	 * @param meetingtime	时间
	 * @param meetingroomname  会议室名称
	 * @param dresscode  西装/工作
	 * @param linkman  联系人
	 * @param linkphone  联系电话
	 * 
	 * @return
	 */
	public static String sendMeetingTemplateSMS(MeetingSms meetingsms){
		String phone = meetingsms.getPhone();
		String temp = meetingsms.getTemplatecode();
		log.info("容联云短信发送会议通知 phone:"+phone+";模版ID:"+temp);
		String res = "";
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.sendTemplateSMS(phone,temp,new String[]{meetingsms.getInitiator(),
				meetingsms.getMeetingname(),meetingsms.getMeetingtime(),meetingsms.getMeetingroomname(),
				meetingsms.getDresscode(),meetingsms.getLinkman(),meetingsms.getLinkphone()});
		
		if("000000".equals(result.get("statusCode"))){			
			res = Boolean.TRUE.toString();
			log.info("荣联云通讯Rsms return result :" +result);
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	
	
	/**
	 * 容联云发送短信验证码
	 * @param phone	手机号码
	 * @param code	验证码
	 * @param time	有效时间(分钟)
	 * @param temp  模版id
	 * @return
	 */
	public static String sendTemplateSMS(String phone,String code,String time,String temp){
		log.info("荣联运通讯发送短消息。phone:"+phone+";code:"+code+";time:"+time+";模版ID:"+temp);
		String res = "";
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.sendTemplateSMS(phone,temp,new String[]{code,time});
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			/*
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}
			*/
			res = Boolean.TRUE.toString();
			log.info("荣联云通讯Rsms return result :" +result);
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	/**
	 * 容联云发送短信验证码
	 * @param phone	手机号码
	 * @param code	验证码
	 * @param time	有效时间(分钟)
	 * @param temp  模版id
	 * @return
	 */
	public static Map<String,Object> sendWindowTemplateSMS(String phone,String code,String time,String temp){
		Map<String,Object> map = new HashMap<String,Object>();
		log.info("荣联运通讯发送短消息。phone:"+phone+";code:"+code+";time:"+time+";模版ID:"+temp);
		String res = "";
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.sendTemplateSMS(phone,temp,new String[]{code,time});
		if("000000".equals(result.get("statusCode"))){
			res = Boolean.TRUE.toString();
			log.info("荣联云通讯Rsms return result :" +result);
			map.put("data", res);
			map.put("message",result.get("statusMsg"));
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
			map.put("data", res);
			map.put("message",result.get("statusMsg") );
		}
		return map;
	}
	
	/**
	 * 容联云，发送语音验证码
	 * @param phone   手机号
	 * @param code    验证码【4-8】位数字或字母
	 * @return
	 */
	public static String sendVoiceVerify(String phone,String code){
		String res = "";		
		HashMap<String, Object> result = null;
		CCPRestSDK restAPI = new CCPRestSDK();
		restAPI.init(proIP, port);// 初始化服务器地址和端口，格式如下，服务器地址不需要写https://
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));// 初始化主帐号和主帐号TOKEN
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.voiceVerify(code,phone,"112233","3","", "", "","","");		
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			/*
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}*/
			log.info("Rsms return result :" +result);
			res = Boolean.TRUE.toString();
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	
	/**
	 * 容联云发送票务信息
	 * @param phone	电话号码
	 * @param str1	票信息a票1张，b票2张
	 * @param str2   012231121，033211342，
	 * @param template 短信模版编码
	 * @return
	 */
	public static String sendOderInfo(String phone,String str1,String str2,String template){		
		String res = "";
		
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.sendTemplateSMS(phone,template,new String[]{str1,str2});
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			/*
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}
			*/
			res = Boolean.TRUE.toString();
			log.info("Rsms return result :" +result);
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	
	
	public static String sendCollectionSMS(String phone,String expresscompany,String expressnum,String code,String temp){
		log.info("荣联运通讯发送短消息。phone:"+phone+";expresscompany:"+expresscompany+";expressnum:"+expressnum+";模版ID:"+temp);
		String res = "";
		HashMap<String, Object> result = null;
		CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
		restAPI.init(proIP,port);
		restAPI.setAccount(PropertiesConstant.CONFIG_PRO.getStr("smsacountid"),PropertiesConstant.CONFIG_PRO.getStr("smsauthtoken"));
		restAPI.setAppId(PropertiesConstant.CONFIG_PRO.getStr("smsappid"));
		result = restAPI.sendTemplateSMS(phone,temp,new String[]{expresscompany,expressnum,code});
		if("000000".equals(result.get("statusCode"))){
			//正常返回输出data包体信息（map）
			/*
			HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
			Set<String> keySet = data.keySet();
			for(String key:keySet){
				Object object = data.get(key);
				System.out.println(key +" = "+object);
			}
			*/
			res = Boolean.TRUE.toString();
			log.info("荣联云通讯Rsms return result :" +result);
		}else{
			//异常返回输出错误码和错误信息
			log.error("Rsms error ,code = "+result.get("statusCode")+"error message :"+result.get("statusMsg"));
			res = Boolean.FALSE.toString();
		}
		return res;
	}
	
	
	public static void main(String[] args) {
		Map<String,Object> para = new 	HashMap<String,Object>();
		para.put("paientname", "李**");
		para.put("visitdate", "2017-03-08");
		para.put("ampm", "上午");
		para.put("hospitalname", "医院");
		para.put("subdeptname", "外科");
		para.put("docname", "李医生");
		para.put("sourceregfee", "200");
		para.put("phone", "13811485041");
		para.put("temp", Rsms.Identyfy_Appointment);
		String code = sendAppointmentTemplateSMS(para);
		if("true".equals(code)){
			System.out.println("********");
		}
	}
	
}
